<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <div class="login">
          <h1>Hello</h1>
          <h3>欢迎来到冰糖雪黎世界</h3>
          <el-form :rules="rules" :model="loginForm" ref="loginForms">
            <el-form-item prop="username">
              <el-input v-model="loginForm.username" :prefix-icon="User" />
            </el-form-item>
            <el-form-item prop="password">
              <el-input
                type="password"
                show-password
                v-model="loginForm.password"
                :prefix-icon="Lock"
              />
            </el-form-item>
            <el-form-item>
              <el-button
                :loading="loading"
                class="login_btn"
                type="primary"
                @click="handleLogin"
              >
                登录
              </el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script lang="ts" setup>
import { User, Lock } from '@element-plus/icons-vue'
import { getTime } from '@/utils/time'
import useUserStore from '@/store/modules/user.ts'
import { reactive, ref } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElNotification } from 'element-plus'
const userStore = useUserStore()
const router = useRouter()
const route = useRoute()
const loading = ref(false)
const loginForms = ref()
const loginForm = reactive({
  username: 'admin',
  password: '111111',
})
const handleLogin = async () => {
  await loginForms.value.validate()
  loading.value = true
  try {
    await userStore.userLogin(loginForm)
    router.push({ path: (route.query.redirect as string) || '/' })
    // router.push({ path: '/' })
    ElNotification({
      type: 'success',
      message: '登录成功',
      title: `${getTime()}好`,
    })
    loading.value = false
  } catch (error) {
    loading.value = false
    ElNotification({
      type: 'error',
      message: (error as Error).message,
    })
  }
}
// 表单自定义规则
const validatorUserName = (rule: any, value: any, callback: any) => {
  if (/^[a-zA-Z0-9]{6,12}$/.test(value)) {
    callback()
  } else {
    callback(new Error('密码要求是6-10位'))
  }
}
const rules = {
  username: [
    {
      required: true,
      min: 5,
      max: 10,
      message: '账号长度5-10位',
      trigger: 'change',
    },
  ],
  password: [{ trigger: 'change', validator: validatorUserName }],
}
</script>

<style lang="scss" scoped>
.login_container {
  width: 100%;
  height: 100vh;
  background: url(../../assets/images/background.jpg) no-repeat;
  background-size: cover;
  .login {
    position: relative;
    width: 80%;
    padding: 40px;
    top: 30vh;
    background: url('../../assets/images/login_form.png') no-repeat;
    background-size: cover;
    h1,
    h3 {
      color: white;
    }
    h1 {
      font-size: 40px;
      padding: 10px;
    }
    h3 {
      font-size: 20px;
      padding: 15px;
    }
    .login_btn {
      width: 100%;
    }
  }
}
</style>
